//
//  LoginManager.swift
//  covermedia
//
//  Created by AlienLi on 16/2/29.
//  Copyright © 2016年 covermedia. All rights reserved.
//

import UIKit
import RealmSwift
import ObjectMapper

public enum LoginState: Int {
    case unlogin = 0
    case normal
    case third
}


open class LoginManager: NSObject {
    
    public static let shareManager = LoginManager()
    
    public var successHandler: ((Bool) -> Void)?
    public var cancelHandler: ((Void) -> Void)?
    fileprivate override init() {}
    
    public var loginState: LoginState {
        set {
            UserDefaults.standard.set(newValue.rawValue, forKey: "CM_login_state")
        }
        get {
            let temp = UserDefaults.standard.integer(forKey: "CM_login_state")
            let state = LoginState(rawValue: temp)
            return state!
        }
    }

    public var currentUserID: Int {
        set {
            UserDefaults.standard.set(newValue, forKey: "CM_Login_User_id")
        }
        get {
            return UserDefaults.standard.integer(forKey: "CM_Login_User_id")
        }
    }
    
    public func postLoginSuccessNofition(_ isRegist:Bool, sender:AnyObject? = nil) {
        CMNotification.notification(.loginLoginSusscess, object: sender, userInfo: ["isRegist": isRegist]).post()
    }

}

open class CMUserProfile:NSObject,NSCoding,Mappable {

    public var user_id: Int = -1
    public var user_name: String?
    public var avatar: String?
    public var nickname: String?
    public var mobile: String?
    public var first_name: String?
    public var last_name: String?
    public var email: String?
    public var age: Int = -1
    public var gender: String?
    public var birthday: Int = -1
    public var is_qq: Bool = false
    public var is_weibo: Bool = false
    public var is_wechat: Bool = false
    public var registeer_way: Int = -1
    public var token: String?
    
    
    public override init() {
        super.init()
    }
    
    public required convenience init?(map: Map) {
        self.init()
    }
    
    public convenience init(entityProfile:CMEntityProfile) {
        self.init()
        user_id = entityProfile.user_id
        user_name = entityProfile.user_name
        avatar = entityProfile.avatar
        nickname = entityProfile.nickname
        mobile = entityProfile.mobile
        first_name = entityProfile.first_name
        last_name = entityProfile.last_name
        email = entityProfile.email
        age = entityProfile.age
        gender = entityProfile.gender
        birthday = entityProfile.birthday
        is_qq = entityProfile.is_qq
        is_weibo = entityProfile.is_weibo
        is_wechat = entityProfile.is_wechat
        registeer_way = entityProfile.registeer_way
        token = entityProfile.token
    }
    
    public func mapping(map: Map) {
        user_id <- map["user_id"]
        user_name <- map["user_name"]
        avatar <- map["avatar"]
        nickname <- map["nickname"]
        mobile <- map["mobile"]
        first_name <- map["first_name"]
        last_name <- map["last_name"]
        email <- map["email"]
        gender <- map["gender"]
        birthday <- map["birthday"]
        is_qq <- map["is_qq"]
        is_weibo <- map["is_weibo"]
        is_wechat <- map["is_wechat"]
        registeer_way <- map["registeer_way"]
        age <- map["age"]
        token <- map["token"]
        
    }
    
    public func encode(with aCoder: NSCoder) {
        
        aCoder.encode(Int64(user_id), forKey: "user_id")
        aCoder.encode(user_name, forKey: "user_name")
        aCoder.encode(avatar, forKey: "avatar")
        aCoder.encode(nickname, forKey: "nickname")
        aCoder.encode(mobile, forKey: "mobile")
        aCoder.encode(first_name, forKey: "first_name")
        aCoder.encode(last_name, forKey: "last_name")
        aCoder.encode(email, forKey: "email")
        aCoder.encode(Int64(age), forKey: "age")
        aCoder.encode(gender, forKey: "gender")
        aCoder.encode(birthday, forKey: "birthday")
        aCoder.encode(is_qq, forKey: "is_qq")
        aCoder.encode(is_weibo, forKey: "is_weibo")
        aCoder.encode(is_wechat, forKey: "is_wechat")
        aCoder.encode(Int64(registeer_way), forKey: "registeer_way")
        aCoder.encode(token, forKey: "token")

    }
    
    public required init?(coder aDecoder: NSCoder) {
        user_id = Int(aDecoder.decodeInt64(forKey: "user_id"))
        user_name = aDecoder.decodeObject(forKey: "user_name") as? String
        avatar = aDecoder.decodeObject(forKey: "avatar") as? String
        nickname = aDecoder.decodeObject(forKey: "nickname") as? String
        mobile = aDecoder.decodeObject(forKey: "mobile") as? String
        first_name = aDecoder.decodeObject(forKey: "first_name") as? String
        last_name = aDecoder.decodeObject(forKey: "last_name") as? String
        email = aDecoder.decodeObject(forKey: "email") as? String
        age = Int(aDecoder.decodeInt64(forKey: "age"))
        gender = aDecoder.decodeObject(forKey: "gender") as? String
        birthday = Int(aDecoder.decodeInt64(forKey: "birthday"))
        is_qq = aDecoder.decodeBool(forKey: "is_qq")
        is_wechat = aDecoder.decodeBool(forKey: "is_wechat")
        is_weibo = aDecoder.decodeBool(forKey: "is_weibo")
        registeer_way = Int(aDecoder.decodeInt64(forKey: "registeer_way"))
        token = aDecoder.decodeObject(forKey: "token") as? String
    }

}

open class CMUserProfileStorageService {
    open static let shared = CMUserProfileStorageService()
    fileprivate let key:String = "CMUesrProfile"

    public func storeUserProfile(_ userProfile:CMUserProfile) {
        let data = NSKeyedArchiver.archivedData(withRootObject: userProfile)
        UserDefaults.standard.set(data, forKey: key + "\(userProfile.user_id)")
        UserDefaults.standard.synchronize()
    }
    
    public func retrieveUserProfile(_ userid:Int) -> CMUserProfile? {
        if let data = UserDefaults.standard.object(forKey: key + "\(userid)") as? Data {
            return NSKeyedUnarchiver.unarchiveObject(with: data) as? CMUserProfile
        }
        return nil
    }

    
}


public extension LoginManager {
    public func getCurrentUser() -> CMUserProfile?{
        if loginState != .unlogin {
            return CMUserProfileStorageService.shared.retrieveUserProfile(currentUserID)
        }
        return nil
    }
}
